package com.sansui.blog.controller.admin.page;

import com.sansui.blog.pojo.ResponseResult;
import com.sansui.blog.pojo.User;
import com.sansui.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @name: TagController
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-03-28 22:44
 **/
//后台标签管理
@Controller
@RequestMapping("/admin")
public class TagController {


    @Autowired
    private TagService tagService;


    //  后台标签管理页面
    @GetMapping("page/tags")
    public String adminTags(){
        return "admin/page/tags";
    }

    //  获取标签接口数据
    @PostMapping("page/getTagList")
    @ResponseBody
    public Map<String, Object> getTagList(Integer page, Integer limit){
        Map<String, Object> map = tagService.getAllTagByPage(page,limit);
        return map;
    }

    @PostMapping("page/getTagList/blogs")
    @ResponseBody
    public Map<String, Object> getTagListByblogs(@RequestParam Map<String, String> paraMap){
        Map<String, Object> map = new HashMap<>(16);
        if(paraMap.get("keyword")==null){
            Integer page = Integer.valueOf(paraMap.get("page"));
            Integer limit = Integer.valueOf(paraMap.get("limit"));
            map = tagService.getAllTagByPage(page,limit);
        }else {
            map = tagService.tagSearchByName(paraMap.get("keyword"));
        }
        return map;
    }


    //  打开添加标签页面
    @GetMapping("page/tag/add")
    public String addTagHtml(){
        return "admin/page/tag/add";
    }

    //  打开编辑标签页面
    @GetMapping("page/tag/edit")
    public String editTagHtml(){
        return "admin/page/tag/edit";
    }


    //  根据标签id删除标签
    @PostMapping("page/deleteTag")
    @ResponseBody
    public Map<String, Object> deleteTag(Long tagId){
        Map<String, Object> map = tagService.deleteTag(tagId);
        return map;
    }


    //  根据标签id批量删除标签
    @PostMapping("page/deleteTagByIds")
    @ResponseBody
    public Map<String, Object> deleteTagByIds(String tagIds){
        List<String> delList = new ArrayList<>();
        String[] tIds = tagIds.split(",");
        for (String str : tIds) {
            delList.add(str);
        };
        Map<String, Object> map = tagService.deleteTagByIds(delList);
        return map;
    }

    //  根据name获取标签信息
    @PostMapping("page/tagSearchByName")
    @ResponseBody
    public Map<String, Object> tagSearchByName(String tagName){
        Map<String, Object> map = tagService.tagSearchByName(tagName);
        return map;
    }

    //  添加标签
    @PostMapping("page/tag/add")
    @ResponseBody
    public Map<String, Object> addTag(String tagName, HttpSession session){
//        User user = (User) session.getAttribute("user");
        Map<String,Object> data = (Map<String, Object>) session.getAttribute("user");
        Map<String, Object> map = tagService.addTag(tagName,String.valueOf(data.get("username")));
        return map;
    }

    //  修改标签
    @PostMapping("page/tag/update")
    @ResponseBody
    public Map<String, Object> updateTag(String tagName, Long tagId,HttpSession session){
//        User user = (User) session.getAttribute("user");
        Map<String,Object> data = (Map<String, Object>) session.getAttribute("user");
        Map<String, Object> map = tagService.updateTag(tagName,tagId,String.valueOf(data.get("username")));
        return map;
    }




}
